package group.igfd.manager.biz.controller;

import java.util.*;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import group.igfd.manager.biz.entity.CategoriesEntity;
import group.igfd.manager.biz.service.CategoriesService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 分类表
 *
 * @author knight
 * @email sunlightcs@gmail.com
 * @date 2018-10-18 14:01:08
 */
@RestController
@RequestMapping("biz/categories")
@Api(tags = {"A001/ 分类管理"})
public class CategoriesController {
    @Autowired
    private CategoriesService categoriesService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("biz:categories:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoriesService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("biz:categories:info")
    public R info(@PathVariable("id") Integer id){
			CategoriesEntity categories = categoriesService.selectById(id);

        return R.ok().put("categories", categories);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("biz:categories:save")
    public R save(@RequestBody CategoriesEntity categories){
			categoriesService.insert(categories);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("biz:categories:update")
    public R update(@RequestBody CategoriesEntity categories){
			categoriesService.updateById(categories);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("biz:categories:delete")
    public R delete(@RequestBody Integer[] ids){
			categoriesService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 列表
     */
    @GetMapping("/listNew")
    //@RequiresPermissions("biz:publishCustomers:list")
    @ApiOperation(value = "获取分类列表")
    public R getCategoryList(){
        List<Map> resultList = new ArrayList<>();

        EntityWrapper<CategoriesEntity> categoriesEntityEntityWrapperParent = new EntityWrapper<>();
        categoriesEntityEntityWrapperParent.eq("father_id",0);
        List<CategoriesEntity> categoriesEntities = categoriesService.selectList(categoriesEntityEntityWrapperParent);

        for (CategoriesEntity categoriesEntity : categoriesEntities) {
            Map<String, Object> categoryChildren = new HashMap<>();

            categoryChildren.put("id",categoriesEntity.getId());
            categoryChildren.put("name",categoriesEntity.getName());
            categoryChildren.put("parentId",categoriesEntity.getFatherId());

            EntityWrapper<CategoriesEntity> categoriesEntityEntityWrapperChildren = new EntityWrapper<>();
            categoriesEntityEntityWrapperChildren.eq("father_id",categoriesEntity.getId());
            List<CategoriesEntity> categoriesEntitiesChildren = categoriesService.selectList(categoriesEntityEntityWrapperChildren);
            List<Map> resultChildrenList = new ArrayList<>();

            for (CategoriesEntity categoriesEntityChildren:categoriesEntitiesChildren) {
                Map<String, Object> category2 = new HashMap<>();

                category2.put("id",categoriesEntityChildren.getId());
                category2.put("name",categoriesEntityChildren.getName());
                category2.put("parentId",categoriesEntityChildren.getFatherId());
                resultChildrenList.add(category2);
            }
            categoryChildren.put("children",resultChildrenList);

            resultList.add(categoryChildren);
        }

        return R.ok().put("categoryList", resultList);
    }




}
